﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using POS.BusinessComponents;
using POS.BusinessEntities;
using POS.UI.Mantenimientos;

namespace POS.UI.UserControls
{
    public partial class SearchableTextBox : UserControl
    {
        public SearchableTextBox()
        {
            InitializeComponent();
        }

        #region Declarations

        public event EventHandler SelectionChangedEvent;
        public event EventHandler ValidatedEvent;
        
        private SearchField searchBy = SearchField.Name;

        #endregion
        
        #region Properties: Public

        public int SelectedId { get; set; }
        public string SelectedCode { get; set; }
        public string SelectedName { get; set; }

        public override string Text
        {
            get { return textBox.Text; }
            set { textBox.Text = value; }
        }

        [Category("Data")]
        [DisplayName("Entidad")]
        public TipoEntidad Entidad { get; set; }
        
        public SearchField SearchBy
        {
            get { return searchBy; }
            set { searchBy = value; }
        }

        #endregion

        #region Event Handlers

        private void SearchableTextBox_Load(object sender, EventArgs e)
        {
            ConfigureView();
        }

        private void SearchableTextBox_SizeChanged(object sender, EventArgs e)
        {
            const int buttonWidth = 20;
            const int labelWidth = 10;
            textBox.Size = new Size(Size.Width - buttonWidth - labelWidth - 2, Size.Height);
            button.Size = new Size(buttonWidth, Size.Height);
            invalidoLabel.Size = new Size(labelWidth, Size.Height);
            button.Location = new Point(Size.Width - buttonWidth - labelWidth, 0);
            invalidoLabel.Location = new Point(Size.Width - labelWidth, 0);
            Refresh();
        }

        private void button_MouseHover(object sender, EventArgs e)
        {
            button.BackgroundImage = Imagenes.qfindhover;
        }

        private void button_MouseLeave(object sender, EventArgs e)
        {
            button.BackgroundImage = Imagenes.qfind;
        }

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
                button.PerformClick();
        }

        private void button_Click(object sender, EventArgs e)
        {
            IBusquedaForm form;
            if (Entidad == TipoEntidad.Articulo)
                form = new BusquedaArticuloForm(textBox.Text.Trim(), SearchBy);
            else
                form = new BusquedaForm(Entidad, textBox.Text.Trim());
            
            if (form.ShowDialog(ParentForm) == DialogResult.OK)
            {
                AsignaSeleccion(new BEEntidad { Id = form.SelectedId, Nombre = form.SelectedName });
                textBox.Text = form.SelectedName;
            }
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            LimpiaSeleccion();
        }

        private void SearchableTextBox_Validated(object sender, EventArgs e)
        {
            string filtro = textBox.Text.Trim();
            if (!String.IsNullOrEmpty(filtro))
            {
                var entidad = new BCEntidad(Entidad).GetItemByName(filtro);
                if (entidad != null)
                {
                    AsignaSeleccion(entidad);
                    textBox.Text = entidad.Nombre;
                    invalidoLabel.Visible = false;
                }
                else
                {
                    invalidoLabel.Visible = true;
                    LimpiaSeleccion();
                }
            }

            if (ValidatedEvent != null)
                ValidatedEvent(sender, e);
        }

        #endregion

        #region Methods: Non-Public

        private void ConfigureView()
        {
            button.BackgroundImage = Imagenes.qfind;
        }

        private void AsignaSeleccion(BEEntidad item)
        {
            SelectedId = item.Id;
            SelectedName = item.Nombre;
            if (SelectionChangedEvent != null)
                SelectionChangedEvent(this, new EventArgs());
        }

        private void LimpiaSeleccion()
        {
            SelectedId = -1;
            SelectedCode = "";
            SelectedName = "";
            if (SelectionChangedEvent != null)
                SelectionChangedEvent(this, new EventArgs());
        }

        #endregion
    }
}
